import os
from cryptography.fernet import Fernet

def encrypt_file(file_path, key):
  if not os.path.isfile(file_path):
    print(f"File '{file_path}' does not exist.")
    return
  with open(file_path, "rb") as file:
    data = file.read()
  encrypted_data = encrypt_data(data, key)
  encrypted_file_path = file_path + ".enc"
  with open(encrypted_file_path, "wb") as file:
    file.write(encrypted_data)
  print(
    f"Encryption complete. Encrypted file saved as '{encrypted_file_path}'.")

def decrypt_file(file_path, key):
  if not os.path.isfile(file_path):
    print(f"File '{file_path}' does not exist.")
    return
  with open(file_path, "rb") as file:
    data = file.read()
  decrypted_data = decrypt_data(data, key)
  decrypted_file_path = os.path.splitext(file_path)[0]
  with open(decrypted_file_path, "wb") as file:
    file.write(decrypted_data)
  print(
    f"Decryption complete. Decrypted file saved as '{decrypted_file_path}'.")

def encrypt_data(data, key):
  cipher = Fernet(key)
  encrypted_data = cipher.encrypt(data)
  return encrypted_data

def decrypt_data(data, key):
  cipher = Fernet(key)
  decrypted_data = cipher.decrypt(data)
  return decrypted_data